# -*- coding:utf-8 -*-

from fastapi import APIRouter, Depends, Form
from sqlalchemy.ext.asyncio import AsyncSession
from starlette.requests import Request
from typing import List
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.entity.vo.import_vo import ImportModel
from module_admin.service.import_service import ImportService
from module_admin.service.login_service import LoginService
from module_admin.aspect.data_scope import GetDataScope
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.annotation.log_annotation import Log
from utils.response_util import ResponseUtil
from utils.common_util import bytes2file_response

from module_product.entity.vo.bg_order_vo import BgOrderPageModel, BgOrderModel, BgOrderCreateModel, PhoneMsgReq
from module_product.service.bg_order_service import BgOrderService

bgOrderController = APIRouter(prefix='/bg/order')


@bgOrderController.get('/list', dependencies=[Depends(CheckUserInterfaceAuth('bg:order:query'))])
async def get_bg_order_list(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
        page_query: BgOrderPageModel = Depends( BgOrderPageModel.as_query),
        data_scope_sql: str = Depends(GetDataScope('BgOrder'))
):
    bg_order_result = await BgOrderService.get_bg_order_list(query_db, page_query, data_scope_sql)

    return ResponseUtil.success(model_content=bg_order_result)


@bgOrderController.get('/userOrder', dependencies=[Depends(CheckUserInterfaceAuth('bg:order:query'))])
async def get_bg_user_order_list(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
        page_query: BgOrderPageModel = Depends( BgOrderPageModel.as_query),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),

):

    bg_order_result = await BgOrderService.get_bg_user_order_list(query_db, page_query, current_user.user.user_id)

    return ResponseUtil.success(model_content=bg_order_result)

@bgOrderController.get('/getById/{bgOrderId}', dependencies=[Depends(CheckUserInterfaceAuth('bg:order:query'))])
async def get_bg_order_by_id(
        request: Request,
        bgOrderId: int,
        query_db: AsyncSession = Depends(get_db),
        data_scope_sql: str = Depends(GetDataScope('BgOrder')),
):
    bg_order = await BgOrderService.get_bg_order_by_id(query_db, bgOrderId)
    return ResponseUtil.success(data=bg_order)

@bgOrderController.get('/statistics', dependencies=[Depends(CheckUserInterfaceAuth('bg:order:query'))])
async def get_order_statistics(
        request: Request,
        query_db: AsyncSession = Depends(get_db)
):
    """
    获取订单统计信息
    """
    statistics = await BgOrderService.get_order_statistics(query_db)
    return ResponseUtil.success(data=statistics)


@bgOrderController.post('/add', dependencies=[Depends(CheckUserInterfaceAuth('bg:order:add'))])
@Log(title='bg_order', business_type=BusinessType.INSERT)
async def add_bg_order (
    request: Request,
    add_model: BgOrderCreateModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):


    add_dict_type_result = await BgOrderService.add_bg_order(query_db, request.app.state.redis, add_model, current_user.user)
    return ResponseUtil.success(data=add_dict_type_result)

@bgOrderController.put('/update', dependencies=[Depends(CheckUserInterfaceAuth('bg:order:edit'))])
@Log(title='bg_order', business_type=BusinessType.UPDATE)
async def update_bg_order(
    request: Request,
    edit_model: BgOrderModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_dict_type_result = await BgOrderService.update_bg_order(query_db, edit_model)
    return ResponseUtil.success(data=add_dict_type_result)


@bgOrderController.delete('/delete/{bgOrderIds}', dependencies=[Depends(CheckUserInterfaceAuth('bg:order:remove'))])
@Log(title='bg_order', business_type=BusinessType.DELETE)
async def del_bg_order(
    request: Request,
    bgOrderIds: str,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    ids = bgOrderIds.split(',')
    del_result = await BgOrderService.del_bg_order(query_db, ids)
    return ResponseUtil.success(data=del_result)

@bgOrderController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('bg:order:export'))])
@Log(title='bg_order', business_type=BusinessType.EXPORT)
async def export_bg_order(
    request: Request,
    bg_order_form: BgOrderPageModel = Form(),
    query_db: AsyncSession = Depends(get_db),
    data_scope_sql: str = Depends(GetDataScope('BgOrder')),
):
    # 获取全量数据
    export_result = await BgOrderService.export_bg_order_list(
        query_db, bg_order_form, data_scope_sql
    )
    return ResponseUtil.streaming(data=bytes2file_response(export_result))

@bgOrderController.post('/import', dependencies=[Depends(CheckUserInterfaceAuth('bg:order:import'))])
async def import_bg_order(request: Request,
                      import_model: ImportModel,
                      query_db: AsyncSession = Depends(get_db),
                      current_user: CurrentUserModel = Depends(LoginService.get_current_user)
    ):
    """
    导入数据
    """
    await ImportService.import_data(query_db, import_model, current_user)
    return ResponseUtil.success()


@bgOrderController.post('/receiveMsg')
async def receive_pay_msg(request: Request, msg_model: PhoneMsgReq, query_db: AsyncSession = Depends(get_db),):
    await BgOrderService.receive_pay_msg(query_db, msg_model)
    return ResponseUtil.success()